#ifndef CONSTANTPOOL_H
#define CONSTANTPOOL_H
#include <vector>
#include <stdio.h>
#include "jtypes.h"
#include "ClassFileConstant.h"
class ConstantPool
{
  public:
    ConstantPool();

    u2 addFromFile(FILE *f);

    ClassFileConstant * getAt(int index);

    void save(FILE *f);

    int inline size() { return m_constants.size(); };

    inline void dump() { for(u2 i=1;i<m_constants.size();i++) { printf("#%03d - ",i); m_constants[i]->dump(this,true); printf("\n"); } }

    /* constant type check */
    bool isStringAt(u2 index);
    bool isUtf8At(u2 index);
    bool isClassAt(u2 index);

    /* adding constants */
    u2 addUtf8(const char * cstring);

    u2 getUtf8(const char * cstring);

    u2 getString(u2 utf8_index);

    u2 addClass(const char * cstring);

    u2 getClass(const char * cstring);

    u2 addNameAndType(const char * name, const char * signature);

    u2 getNameAndType(const char * name, const char * signature);

    u2 addFieldRef(const char * class_name, 
        const char * field_name,
        const char * signature);
    u2 addMethodRef(const char * class_name, 
        const char * field_name,
        const char * signature);

    u2 getFieldRef(u2 class_index, u2 NameAndType_index);
    u2 getMethodRef(u2 class_index, u2 NameAndType_index);


    u2 addString(const char * cstring);

    u2 addDouble(double);


    /* querying constants */
    const char * getCString(u2 index);
    
    std::vector<ClassFileConstant*> m_constants;
};
#endif
